package eerbaar.collision.internal;

import eerbaar.collision.IPetEventService;
import eerbaar.elements.IElement;
import eerbaar.message.lib.NotifyPetAtEdgeMsg;
import eerbaar.position.IPosition;
import eerbaar.shared.message.MsgHelper;

public class PetEventService implements IPetEventService {

    private boolean isBugPresent(IElement[] elements) {

        boolean result = false;
        for (IElement e : elements) {
            if (e.getName().equals("bug")) {
                result = true;
                break;
            }
        }

        return result;
    }

    private boolean isEdge(IPosition pos) {

        if (pos.getX() == 0 || pos.getX() == 9 || pos.getY() == 0
                || pos.getY() == 9) {

            return true;
        }

        return false;
    }

    @Override
    public void process(IElement[] elements, IPosition pos1, IPosition pos2) {

        if (!isBugPresent(elements)) {
            return;
        }

        boolean edge1 = isEdge(pos1);
        boolean edge2 = isEdge(pos2);
        if (edge2 && !edge1) {

            MsgHelper.sendMsg(NotifyPetAtEdgeMsg.class,
                    new NotifyPetAtEdgeMsg());
        }
    }

}
